/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-28 11:37:31
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-28 14:20:03
 * @Description: file content
 */

#include "apue.h"

#define BUFFSIZE 1024

static void sig_tstp(int signo) // signal handler for SIGTSTP
{
    sigset_t    mask;

    // ... move cursor to lower left corner, reset tty mode ...
    // Unblock SIGTSTP, since it's blocked while we're handling it.
    sigemptyset(&mask);
    sigaddset(&mask, SIGTSTP);
    sigprocmask(SIG_UNBLOCK, &mask, NULL);

    signal(SIGTSTP, SIG_DFL);   // reset disposition to default
    kill(getpid(), SIGTSTP);    // and send the signal to ourself

    // we don't return from the kill until we're continued
    signal(SIGTSTP, sig_tstp);  // reestablish signal handler
    // .. reset tty mode, redraw screen ...
}

int main(void)
{
    int n = 0;
    char buf[BUFFSIZE] = {0};

    // Only catch SIGTSTP if we're running with a job-control shell

    if (SIG_DFL == signal(SIGTSTP, SIG_IGN))
    {
        signal(SIGTSTP, sig_tstp);
    }

    while (1)
    {
        n = read(STDIN_FILENO, buf, BUFFSIZE);
        if (n <= 0)
        {
            break;
        }

        if (n != write(STDOUT_FILENO, buf, n))
        {
            err_sys("write error");
        }
    }

    if (n < 0)
    {
        err_sys("read error");
    }

    exit(0);
}
